package com.example.zxlwork.fragmet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.zxlwork.R;
import com.example.zxlwork.adapter.RecommendAdapter;
import com.example.zxlwork.recemmend.fagment.AttentionFragment;
import com.example.zxlwork.recemmend.fagment.recFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class RecommendFragment extends Fragment {

    //声明tab标签
    private TabLayout recommend_tab;
    //声明viewpage2标签
    private ViewPager2 recommend_vp2;
    //声明tab和viewpage2的适配器
    private RecommendAdapter recommendAdapter;
    //声明子页面数据源
    private List<Fragment> recommendList;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //绑定布局文件
        View recemmendPage = inflater.inflate(R.layout.fragment_recemmend,null);

        //绑定控件对象
        recommend_tab = recemmendPage.findViewById(R.id.recommend_tab);
        recommend_vp2 = recemmendPage.findViewById(R.id.recommend_vp2);

        //初始化子页面资源
        recommendList = new ArrayList<>();
        recommendList.add(new recFragment());
        recommendList.add(new AttentionFragment());


        //初始化子页面适配器对象
        recommendAdapter = new RecommendAdapter(recommendList,this);
        //选型卡切换滑动方向
        recommend_vp2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //给viewpage2绑定适配器
        recommend_vp2.setAdapter(recommendAdapter);
        //关联tab和viewpage2
        TabLayoutMediator mediator = new TabLayoutMediator(
                recommend_tab,
                recommend_vp2,
                new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        //设置标签的名称
                        switch (position){
                            case 0:
                                tab.setText("推荐");
                                break;
                            case 1:
                                tab.setText("关注");
                                break;

                        }

                    }
                }
        );
        //使tab和viewpage2关联
        mediator.attach();


        return recemmendPage;
    }


}
